package net.sudot.chess.business.dao;

import net.sudot.chess.business.model.SocialUser;
import org.springframework.stereotype.Repository;

import javax.persistence.TypedQuery;

/**
 * 社会化用户
 *
 * @author tangjialin on 2018-03-15.
 */
@Repository
public class SocialUserDao extends BaseDao<SocialUser> {

    /**
     * 获取社会化用户
     *
     * @param loginPluginId 登录插件ID
     * @param uniqueId      唯一ID
     * @return 社会化用户，若不存在则返回null
     */
    public SocialUser findByUniqueId(String loginPluginId, String uniqueId) {
        String ql = "SELECT socialUser FROM SocialUser socialUser WHERE socialUser.loginPluginId= :loginPluginId AND socialUser.uniqueId = :uniqueId";
        TypedQuery<SocialUser> query = entityManager.createQuery(ql, SocialUser.class);
        query.setParameter("loginPluginId", loginPluginId);
        query.setParameter("uniqueId", uniqueId);
        return uniqueResult(query);
    }

    /**
     * 获取社会化用户
     *
     * @param loginPluginId 登录插件ID
     * @param userId        用户主键
     * @return 社会化用户，若不存在则返回null
     */
    public SocialUser findByUser(String loginPluginId, Long userId) {
        String ql = "SELECT socialUser FROM SocialUser socialUser WHERE socialUser.loginPluginId= :loginPluginId AND socialUser.userId = :userId";
        TypedQuery<SocialUser> query = entityManager.createQuery(ql, SocialUser.class);
        query.setParameter("loginPluginId", loginPluginId);
        query.setParameter("userId", userId);
        return uniqueResult(query);
    }

}
